﻿// EditorApp.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;

using Avalon.Windows.Controls;

using Supremacy.Client;
using Supremacy.Game;
using Supremacy.Resources;
using Supremacy.Types;
using System.Windows.Documents;
using System.Windows.Controls;
using DevComponents.WpfRibbon;

namespace Supremacy.Editor
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class EditorApp : INotifyPropertyChanged
    {
        public event EventHandler<ParameterEventArgs<EditorContext>> EditorContextChanged;

        #region Fields
        private EditorContext _editorContext;
        #endregion

        #region Constructors
        static EditorApp()
        {
            AppDomain.CurrentDomain.SetData(
                "DataDirectory",
                Environment.CurrentDirectory);

            //TextElement.FontFamilyProperty.OverrideMetadata(
            //    typeof(Control),
            //    new FrameworkPropertyMetadata(new FontFamily("Brush Script MT") ?? SystemFonts.MessageFontFamily));
        }
        #endregion

        #region Properties
        public static new EditorApp Current
        {
            get { return Application.Current as EditorApp; }
        }

        public static EditorApp CurrentEditor
        {
            get { return Application.Current as EditorApp; }
        }

        public EditorContext EditorContext
        {
            get { return _editorContext; }
        }
        #endregion

        #region Methods
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            try
            {
                ImageCache.Current.ForceCaching = true;
                LoadContext(null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    ex.Message,
                    "Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                Shutdown();
            }
        }

        public void CloseContext()
        {
            CloseContext(true);
        }

        public bool CloseContext(bool confirm)
        {
            EditorContext context = _editorContext;
            if (context != null)
            {
                if (confirm)
                {
                    TaskDialogResult result = TaskDialog.Show(
                        MainWindow,
                        "Save Configuration",
                        "You may have made changes to the current game configuration."
                            + Environment.NewLine
                            + "Do you want to save your changes?",
                        MainWindow.Title,
                        TaskDialogButtons.None,
                        TaskDialogIcon.Question,
                        true,
                        new TaskDialogButtonData(
                            TaskDialogButtons.Yes,
                            "Save Changes",
                            "Your changes will be saved and will appear in the game.",
                            true),
                        new TaskDialogButtonData(
                            TaskDialogButtons.No,
                            "Discard Changes",
                            "Your changes will be discarded.  You will not be able to recover them.",
                            false),
                        new TaskDialogButtonData(
                            TaskDialogButtons.Cancel,
                            "Do Not Close",
                            "The current configuration will not be closed.  You may review your"
                                + Environment.NewLine
                                + "current changes and make more changes if you wish.",
                            false));
                    if (result.StandardButton == TaskDialogButtons.Cancel)
                    {
                        return false;
                    }
                    if (result.StandardButton == TaskDialogButtons.Yes)
                    {
                        context.Save();
                    }
                }
                _editorContext = null;
                OnEditorContextChanged();
                context.Close();
            }
            return true;
        }

        public bool LoadContext(GameMod mod)
        {
            CloseContext(true);
            if (GameContext.Current == null)
                GameContext.Push(new GameContext());
            GameContext.Current.GameMod = mod;
            _editorContext = new EditorContext();
            OnEditorContextChanged();
            return false;
        }
        #endregion

        protected void OnEditorContextChanged()
        {
            if (EditorContextChanged != null)
                EditorContextChanged(this, new ParameterEventArgs<EditorContext>(this.EditorContext));
            OnPropertyChanged("EditorContext");
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}